package org.ztr.yanai.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.ztr.yanai.blog.entity.Comment;
import org.ztr.yanai.blog.enums.CommentStatusEnum;
import org.ztr.yanai.blog.mapper.CommentMapper;
import org.ztr.yanai.blog.service.CommentService;

import java.util.List;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/22 10:00
 * @Description: 评论服务实现类
 * @Version: 1.0
 */
@Service
public class CommentServiceImpl implements CommentService {

    private final CommentMapper commentMapper;

    public CommentServiceImpl(CommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    @Override
    public boolean createComment(Comment comment) {
        return commentMapper.insert(comment) > 0;
    }

    @Override
    public boolean deleteCommentById(Long id) {
        return commentMapper.deleteById(id) > 0;
    }

    @Override
    public Comment getCommentById(Long id) {
        return commentMapper.selectById(id);
    }

    @Override
    public Page<Comment> listCommentsByPage(int pageNum, int pageSize, QueryWrapper<Comment> wrapper) {
        Page<Comment> page = new Page<>(pageNum, pageSize);
        return commentMapper.selectPage(page, wrapper);
    }

    @Override
    public boolean updateComment(Comment comment) {
        return commentMapper.updateById(comment) > 0;
    }

    @Override
    public boolean batchUpdateCommentStatus(List<Long> commentIds, CommentStatusEnum status) {
        return commentIds.stream()
                .allMatch(id -> {
                    Comment comment = new Comment();
                    comment.setId(id);
                    comment.setStatus(status);
                    return commentMapper.updateById(comment) > 0;
                });
    }
}
